package org.xhnews.parsers;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Vector;

import org.xhnews.log.RaspberryLogger;
import org.xhnews.to.NewsChannel;
import org.xhnews.to.NewsItem;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

public class NewsPullXMLParser {

	// 解析频道列表
	public static Vector readXHChannels(InputStream inputStream,
			String inputEncoding) {
		try {
			// 创建Pull解析
			XmlPullParserFactory pullParserFactory = XmlPullParserFactory
					.newInstance();
			XmlPullParser pullParser = pullParserFactory.newPullParser();
			// 解析XML
			pullParser.setInput(inputStream, inputEncoding);
			// 开始
			int eventType = pullParser.getEventType();
			Vector channels = null;
			NewsChannel ch = null;
			while (eventType != XmlPullParser.END_DOCUMENT) {
				String nodeName = pullParser.getName();
				switch (eventType) {
				// 文档开始
				case XmlPullParser.START_DOCUMENT:
					channels = new Vector();
					break;
				// 节点开始
				case XmlPullParser.START_TAG:
					if ("column".equals(nodeName)) {
						String title = pullParser.getAttributeValue(null,
								"name");
						String fileName = pullParser.getAttributeValue(null,
								"file");
						String maxid = pullParser.getAttributeValue(null,
								"maxid");
						if (title != null && fileName != null && maxid != null) {
							ch = new NewsChannel();
							ch.setTitle(title);// name
							ch.setFileName(fileName);// file
							ch.setLink(fileName);// file
							ch.setMaxNewsID(maxid);
						} else {
							ch = null;
						}
					}
					break;
				// 节点结束
				case XmlPullParser.END_TAG:
					if ("column".equals(nodeName)) {
						if (ch != null) {
							channels.addElement(ch);
							ch = null;
						}
					}
					break;
				}
				eventType = pullParser.next();
			}// end while
			return channels;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			return null;
		}
	}

	// 解析资讯列表
	public static Vector readXHItems(NewsChannel newschannel,InputStream inputStream,
			String inputEncoding) throws Exception {
		// 创建Pull解析
		XmlPullParserFactory pullParserFactory = XmlPullParserFactory
				.newInstance();
		XmlPullParser pullParser = pullParserFactory.newPullParser();
		// 解析XML
		Reader reader = new InputStreamReader(inputStream, inputEncoding);
		reader.read();
		pullParser.setInput(reader);
		// 开始
		int eventType = pullParser.getEventType();
		Vector items = null;
		NewsItem item = null;
		boolean skipFirstTitle = false;

		try {
			while (eventType != XmlPullParser.END_DOCUMENT) {
				String nodeName = pullParser.getName();
				switch (eventType) {
				// 文档开始
				case XmlPullParser.START_DOCUMENT:
					items = new Vector();
					break;
				// 节点开始
				case XmlPullParser.START_TAG:
					if ("title".equals(nodeName)) {
						if (!skipFirstTitle) {// 已经跳过第一个title(是频道的title)
							skipFirstTitle = true;// 跳过第一个title
						} else {// 已经跳过第一个title
							item.setTitle(pullParser.nextText());
						}
					} else if ("item".equals(nodeName)) {
						// 资讯节点开始
						item = new NewsItem();
					} else if ("link".equals(nodeName)) {
						item.setFileName(pullParser.nextText());
					} else if ("id".equals(nodeName)) {
						item.setID(pullParser.nextText());

					} else if ("pubDate".equals(nodeName)) {
						item.setPubDate(pullParser.nextText());
					} else if ("attach".equals(nodeName)) {
						String raw = pullParser.nextText();

						int end = raw.indexOf(":");
						if (end != -1) {
							item.setAttach(raw);
						} else {
							item.setAttach("none");
						}
					}
					break;
				// 节点结束
				case XmlPullParser.END_TAG:
					if ("item".equals(nodeName)) {
						item.setChannelTitle(newschannel.getTitle());
						item.setChannelFileName(newschannel.getFileName());
						items.addElement(item);
						item = null;
					}
					break;
				}
				eventType = pullParser.next();
			}// end while
			return items;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			e.printStackTrace();
			return null;
		}
	}
	// 解析资讯列表
	public static Vector readVideoItems(InputStream inputStream,
			String inputEncoding) throws Exception {
		// 创建Pull解析
		XmlPullParserFactory pullParserFactory = XmlPullParserFactory
				.newInstance();
		XmlPullParser pullParser = pullParserFactory.newPullParser();
		// 解析XML
		Reader reader = new InputStreamReader(inputStream, inputEncoding);
		reader.read();
		pullParser.setInput(reader);
		// 开始
		int eventType = pullParser.getEventType();
		Vector items = null;
		NewsItem item = null;
		boolean skipFirstTitle = false;

		try {
			while (eventType != XmlPullParser.END_DOCUMENT) {
				String nodeName = pullParser.getName();
				switch (eventType) {
				// 文档开始
				case XmlPullParser.START_DOCUMENT:
					items = new Vector();
					break;
				// 节点开始
				case XmlPullParser.START_TAG:
					if ("title".equals(nodeName)) {
						if (!skipFirstTitle) {// 已经跳过第一个title(是频道的title)
							skipFirstTitle = true;// 跳过第一个title
						} else {// 已经跳过第一个title
							item.setTitle(pullParser.nextText());
						}
					} else if ("item".equals(nodeName)) {
						// 资讯节点开始
						item = new NewsItem();
					} else if ("link".equals(nodeName)) {
						item.setFileName(pullParser.nextText());
					} else if ("id".equals(nodeName)) {
						item.setID(pullParser.nextText());

					} else if ("pubDate".equals(nodeName)) {
						item.setPubDate(pullParser.nextText());
					} else if ("attach".equals(nodeName)) {
						String raw = pullParser.nextText();

						int end = raw.indexOf(":");
						if (end != -1) {
							item.setAttach(raw);
						} else {
							item.setAttach("none");
						}
					}
					break;
				// 节点结束
				case XmlPullParser.END_TAG:
					if ("item".equals(nodeName)) {
						item.setChannelTitle("OnlineVideo");
						items.addElement(item);
						item = null;
					}
					break;
				}
				eventType = pullParser.next();
			}// end while
			return items;
		} catch (Exception e) {
			RaspberryLogger.getInstance().info(e.getMessage());
			e.printStackTrace();
			return null;
		}
	}
}
